package tmacsoftware.ursql;

public class UrSQLEntry
{
    // key of entry
    private String key;
    // value of entry
    private String value;
    // original key of entry (matches key in file)
    private String firstKey;
    // original value of entry (matches value in file)
    private String firstValue;
    // corresponding entity
    private UrSQLEntity entity;

    /**
     * Create new entry
     */
    public UrSQLEntry()
    {
    }

    /**
     * Create a new entry from key/value formatted in a query
     * @param query Query containing key/value pair
     */
    public UrSQLEntry(String query)
    {
        // get key and value from query
        String[] split = query.split(UrSQLController.KEY_VALUE_SEPARATOR);
        // save properties
        this.key = split[0];
        this.value = split[1];
        this.firstKey = this.key;
        this.firstValue = this.value;
    }

    /**
     * Create a new entry with the given key and value
     * @param key Key for entry
     * @param value Value for entry
     */
    public UrSQLEntry(String key, String value)
    {
        this.key = key;
        this.value = value;
        this.firstKey = this.key;
        this.firstValue = this.value;
    }

    /**
     * Create a new entry in the given entity
     * @param key Key for entry
     * @param value Value for entry
     * @param entity Entity containing new entry
     */
    public UrSQLEntry(String key, String value, UrSQLEntity entity)
    {
        this(key, value);
        this.entity = entity;
    }

    /**
     * Get key for entry
     * @return Key for entry
     */
    public String getKey()
    {
        return this.key;
    }

    /**
     * Set key for entry
     * @param key New key for entry
     */
    public void setKey(String key)
    {
        this.key = key;
    }

    /**
     * Get value for entry
     * @return Value for entry
     */
    public String getValue()
    {
        return this.value;
    }

    /**
     * Get value of entry as an integer
     * @return Integer of entry value
     */
    public int getValueAsInt()
    {
        return Integer.valueOf(getValue());
    }

    /**
     * Get value of entry as a long
     * @return Long of entry value
     */
    public long getValueAsLong()
    {
        return Long.valueOf(getValue());
    }

    /**
     * Get value of entry as a float
     * @return Float of entry value
     */
    public float getValueAsFloat()
    {
        return Float.valueOf(getValue());
    }

    /**
     * Get value of entry as a double
     * @return Double of entry value
     */
    public double getValueAsDouble()
    {
        return Double.valueOf(getValue());
    }

    /**
     * Set value for entry
     * @param value New value for entry
     */
    public void setValue(String value)
    {
        this.value = value;
    }

    /**
     * Get original key for entry, matching the database file
     * @return Key for entry
     */
    public String getFirstKey()
    {
        return this.firstKey;
    }

    /**
     * Get original value for entry, matching the database file
     * @return
     */
    public String getFirstValue()
    {
        return this.firstValue;
    }

    /**
     * Get entity containing entry
     * @return Entity containing entry
     */
    public UrSQLEntity getEntity()
    {
        return this.entity;
    }

    /**
     * Set entity containing entry
     * @param entity Entity containing entry
     */
    public void setEntity(UrSQLEntity entity)
    {
        this.entity = entity;
    }
}
